<!--点名记录-->
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="搜索学员" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入学员姓名/手机号"
          clearable
          style="width: 240px"
          @keyup.enter="getList"
        >
          <template #append>
            <el-button :icon="Search" @click="getList" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="课消日期" prop="callDate">
        <el-date-picker
          v-model="callDate"
          type="daterange"
          range-separator="~"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="callDateRangeChange"
          value-format="YYYY-MM-DD"
          style="width: 240px"
        />
      </el-form-item>

      <el-form-item label="所在班级" prop="courseId">
        <el-select
          v-model="queryParams.classId"
          placeholder="请选择所在班级"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in classList"
            :key="item.id"
            :label="item.beOver === '0' ? item.name + '(已结业)' : item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="课程名称" prop="courseId1">
        <el-select
          v-model="queryParams.courseId1"
          placeholder="请选择课程名称"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in courseList"
            :key="item.id"
            :label="item.state === '1' ? item.name + '(已停用)' : item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="消耗方式" prop="courseId">
        <el-select
          v-model="queryParams.courseId"
          placeholder="请选择消耗方式"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in courseList"
            :key="item.id"
            :label="item.state === '1' ? item.name + '(已停用)' : item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="授课方式" prop="isVip">
        <el-select
          v-model="queryParams.isVip"
          placeholder="请选择授课方式"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in edu_teaching_mode"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="主班老师" prop="teacher">
        <el-select
          v-model="queryParams.teacher"
          placeholder="请选择主班老师"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in teacherList"
            :key="item.value"
            :label="
              !item.isJob
                ? item.label + '(已离职)'
                : !item.isTeacher
                ? item.label + '(非授课)'
                : item.label
            "
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="助教老师" prop="assistant">
        <el-select
          v-model="queryParams.assistant"
          placeholder="请选择助教老师"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in teacherList"
            :key="item.value"
            :label="
              !item.isJob
                ? item.label + '(已离职)'
                : !item.isTeacher
                ? item.label + '(非授课)'
                : item.label
            "
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="initPage"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button plain icon="Download" @click="handleExport()">
          导出
        </el-button>
      </el-col>

      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="queryPage"
      ></right-toolbar>
    </el-row>
    <el-alert type="warning" :closable="false">
      <template v-slot:title>
        <span style="font-size: 14px; color: #666">
          课消金额：{{ amount }} 元
        </span>
      </template>
    </el-alert>
    <div style="margin-bottom: 10px"></div>
    <el-table v-loading="loading" :data="list" border>
      <el-table-column
        label="课消时间"
        align="center"
        prop="callTime"
        width="160"
      >
        <template #default="scope">
          {{
            scope.row.callTime &&
            scope.row.callTime.slice(0, 10) +
              "    " +
              scope.row.callTime.slice(11, 19)
          }}
        </template>
      </el-table-column>
      <el-table-column label="学员姓名" align="center" prop="studentName" />
      <el-table-column
        label="手机号"
        align="center"
        prop="mobile"
        width="120"
      />
      <el-table-column label="所在班级" align="center" prop="className" />
      <el-table-column label="课程名称" align="center" prop="courseNameC" />
      <el-table-column label="授课方式" align="center" prop="isVip">
        <template #default="scope">{{
          {
            "0": "1v1授课",
            "1": "班级授课",
          }[scope.row.isVip]
        }}</template>
      </el-table-column>
      <el-table-column label="主班老师" align="center" prop="teacherName" />
      <el-table-column
        label="助教老师"
        align="center"
        prop="assistantName"
        width="160"
      />
      <!-- <el-table-column label="课消类型" align="center" prop="classHour" />
      <el-table-column label="课消来源" align="center" prop="classHour" /> -->
      <el-table-column label="消耗方式" align="center" prop="courseName" />
      <el-table-column label="消耗额度" align="center" prop="classHourPay">
        <template #default="scope">
          {{ "消耗" + scope.row.classHourPay + "课时" }}
        </template>
      </el-table-column>
      <el-table-column label="课消金额" align="center" prop="amount">
        <template #default="scope"> {{ scope.row.amount + "元" }} </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button link type="primary" @click="handleDetail(scope.row)">
            查看详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
      size="small"
    />
  </div>
  <Detail ref="detailRef" />
</template>

<script setup>
import { Search } from "@element-plus/icons-vue";
import {
  getListAPI,
  exportClassAttendanceRecordAPI,
  getCountAPI,
} from "@/api/financial/classAttendanceRecord";
import { getClassListAPI } from "@/api/registrar/class";
import { getCourseListAPI } from "@/api/registrar/course";
import { getPeopleList } from "@/api/studentManagement/index";
import Detail from "./detail.vue";

const { proxy } = getCurrentInstance();
const { edu_teaching_mode } = proxy.useDict("edu_teaching_mode");

const loading = ref(false);
const showSearch = ref(true);
const total = ref(0);
const amount = ref(0);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    callStartTime: null,
    callEndTime: null,
    classId: null,
    courseId: null,
    teacher: null,
    assistant: null,
    courseId1: null,
    courseId1: null,
    isVip: null,
  },
});

const { queryParams } = toRefs(data);

const list = ref([]);
const classList = ref([]);
const teacherList = ref([]);
const courseList = ref([]);
const callDate = ref(null);

// 详情
const detailRef = ref(null);
const selectedItem = ref({});

// 事件------------------------------------------------------------------------------------------------------------

function callDateRangeChange(event) {
  callDate.value = event;
  queryParams.value.callStartTime = event ? event[0] : null;
  queryParams.value.callEndTime = event ? event[1] : null;
}
// 导出
function handleExport() {
  let {
    name,
    callStartTime,
    callEndTime,
    classId,
    courseId,
    teacher,
    assistant,
    courseId1,
    isVip,
  } = queryParams.value;

  let param = {
    name,
    callStartTime,
    callEndTime,
    classId,
    courseId,
    teacher,
    assistant,
    courseId1,
    isVip,
  };

  exportClassAttendanceRecordAPI(param).then((res) => {
    let blob = new Blob([res], {
      type: "application/vnd.ms-excel;charset=utf-8",
    });
    const url = window.URL.createObjectURL(blob),
      link = document.createElement("a");
    link.href = url;
    link.download = "消课记录";
    link.click();
    URL.revokeObjectURL(url); // 释放内存
    proxy.$modal.msgSuccess("导出成功");
  });
}

// 详情
function handleDetail(row) {
  selectedItem.value = row;
  detailRef.value.openW(row);
}

function resetQuery() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    name: null,
    callStartTime: null,
    callEndTime: null,
    classId: null,
    courseId: null,
    teacher: null,
    assistant: null,
  };
  callDate.value = null;

  initPage();
}

// 方法------------------------------------------------------------------------------------------

// 获取课消金额
function getCount() {
  loading.value = true;
  getCountAPI({ ...queryParams.value }).then((response) => {
    amount.value = response.data.amount;
    loading.value = false;
  });
}

// 课程
function getCourseList() {
  loading.value = true;
  getCourseListAPI({
    pageSize: 9999,
    pageNum: 1,
  }).then((response) => {
    courseList.value = response.rows;
    loading.value = false;
  });
}

// 班级
function getClassList() {
  loading.value = true;
  getClassListAPI({ pageSize: 9999, pageNum: 1 }).then((response) => {
    classList.value = response.rows;
    loading.value = false;
  });
}

// 获取教师列表
function getTeacherList() {
  loading.value = true;
  getPeopleList({ isTeach: 1 }).then((response) => {
    teacherList.value = response.data;
    loading.value = false;
  });
}

function initPicker() {
  getCourseList();
  getClassList();
  getTeacherList();
}

function getList() {
  loading.value = true;
  getListAPI({ ...queryParams.value }).then((response) => {
    list.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function queryPage() {
  getList();
  getCount();
}

function initPage() {
  queryParams.value.pageNum = 1;
  getList();
  getCount();
}

// 调用-------------------------------------------------------------------------------------------------------------------
initPicker();
initPage();
</script>

<style lang="scss" scoped>
.info {
  display: inline-flex;
  align-items: center;
  height: 32px;
}
</style>
